import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/dropdownmenu.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

//移库作业
class MoveInSelect extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MoveInSelectState();
  }
}

class MoveInSelectState extends State<MoveInSelect> {
  String intoSelectVal;
  String intoSelectFrame;
  String intoSelectPosition;
  bool intoLoad = true;
  List<dynamic> intoLibraryData = new List();
  List<dynamic> intoSelectFrameData = new List();
  List<dynamic> intoSelectPositionData = new List();
  Color intoLoadBgColor = Colors.white;

  @override
  void initState() {
    intoLibraryData.add(DataSingleton().moveLibraryInfo.item);
    intoSelectVal =DataSingleton().moveLibraryInfo.reservoirAreaID;
    print(intoSelectVal);
    print(DataSingleton().moveLibraryInfo.item.toString());
    getFrame();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "移库作业",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body:
        LoadBuilder(visibility: intoLoad,bgColor: intoLoadBgColor,builder:  Column(
          children: <Widget>[
            Expanded(
              child: Container(
                padding: EdgeInsets.all(15),
                child: Column(
                  children: <Widget>[
                    DropDownMenu(
                      hitText: "选择移入库区",
                      value: intoSelectVal,
                      datas: intoLibraryData,
                      keyItem: {"id": "text"},
                      //datas: libraryData,
                      onChange: (data) {
                       /* setState(() {
                          intoSelectVal = data["value"];
                          intoLoadBgColor = Color(0x66BCBCBC);
                          intoLoad = true;

                        });
                        getFrame();*/
                      },
                    ),
                    DropDownMenu(
                      hitText: "选择移入货架",
                      value: intoSelectFrame,
                      datas: intoSelectFrameData,
                      keyItem: {"id": "text"},
                      onChange: (data) {
                        setState(() {
                          intoSelectFrame = data["value"];
                          intoLoadBgColor = Color(0x66BCBCBC);
                          intoLoad = true;
                          intoSelectPosition = null;
                        });
                        getPosition();
                      },
                    ),
                    DropDownMenu(
                      hitText: "选择移入货位",
                      value: intoSelectPosition,
                      datas: intoSelectPositionData,
                      keyItem: {"id": "value"},
                      onChange: (data) {
                        setState(() {
                          intoSelectPosition = data["value"];
                        });
                        print(data["value"]);
                      },
                    ),
                  ],
                ),
              ),
            ),
            Container(
              width: double.infinity,
              height: 48,
              child: FlatButton(
                color: Color(0XFF00A5F1),
                child: Text(
                  "完成",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  if(intoSelectVal!=null&&intoSelectFrame!=null&&intoSelectPosition!=null){
                    setState(() {
                      intoLoad = true;
                    });
                    upData();
                  }else{
                    TostUtil.showToast("请选择至库位");
                  }
                },
              ),
            ),
          ],
        )
          ,)
    );
  }

  void getLibrary() {
    InternetAPI.getInstance().get("Facilities/PrWarehouseArea/GetAreaTree",
            (data) {
          print(data);
          setState(() {
            intoLibraryData = data["dropdownList"];
            intoLoad = false;
          });
        }, errorCallBack: (err) {}, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'pageNo': 1,
          'pageSize': 400
        });
  }

  void getFrame() {
    InternetAPI.getInstance()
        .get("Facilities/PrWarehouseShelf/GetWarehouseShelfSelectedItemList",
            (data) {
          print(data);
          setState(() {
            intoSelectFrame = null;
            intoSelectPosition = null;
            intoSelectFrameData = data;
            intoLoad = false;
          });
        }, errorCallBack: (err) {
          setState(() {
            intoLoad = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          "areaid": intoSelectVal,
          'pageNo': 1,
          'pageSize': 1000
        });
  }

  void getPosition() {
    InternetAPI.getInstance()
        .get("Facilities/PrWarehouseLocation/GetWarehouseLocationSelectedItemList",
            (data) {
          print(data);
          setState(() {
            intoSelectPositionData=data;
            intoLoad = false;
          });
        }, errorCallBack: (err) {
          setState(() {

            intoLoad = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'areaid': intoSelectVal,
          'shelfid':intoSelectFrame,
          'pageNo': 1,
          'pageSize': 1000
        });
  }

  void upData() {
    InternetAPI.getInstance()
        .post("WMS/MiWarehouseMove/UpdateWarehouseMoveDetailArtificial",
            (data) {
          print(data);
          setState(() {
            intoLoad = false;
          });
          if (data == true) {
            Navigator.pop(context,"ok");
            TostUtil.showToast("移库成功");
          }
        }, errorCallBack: (err) {
          setState(() {
            intoLoad = false;
          });
        }, params: {
          'operaterUserId': DataSingleton().Data.id,
          'tenantId': DataSingleton().Data.tenantId,
          'Warehouseid': DataSingleton().warehouseChoice.id,
          'areaid': DataSingleton().moveLibraryInfo.reservoirAreaID,
          'fromlocationid':DataSingleton().moveLibraryInfo.cargoLocationID,
          'tolocationid':intoSelectPosition,
          //'worktoolcode':DataSingleton().moveLibraryInfo.bean[0]['worktoolcode'],
        });
  }
}
